function [x, y, ye, n] = block_average(t, data, block)
% block average averages data in blocks of size block (units of t)
% and returns the result in x (t) and y (data) and ye (se of y)
%
if(nargin == 0)
   t = [0.0:0.5:11.9];
   t = [t 20:.5:30];
   block = 3;
   data = 0.01*t; % linearly increasing
end;
tn = floor (t ./ block)*block; % make blocked t
x = unique(tn);
for i = 1:length(x)
   if(i == length(x))
      tm = max(t);
   	[yd, yi] = find(t >= x(i) & t <= tm); % to be sure we include the last point
   else
      tm = x(i+1);
	   [yd, yi] = find(t >= x(i) & t < tm); % otherwise blocks are exclusive of each other
   end;
   rt = t(yi);
 %  [yi ;t(yi); rt; data(yi)]'
  ui = find(yi < length(data));
  yi = yi(ui);
  n(i) = length(data(yi));
   [y(i) ye(i)] = mean_var(data(yi)); % compute mean/variance - excluding NaN's
   if(n(i) > 1)
      ye(i) = sqrt(ye(i))/(n(i)-1); % return as standard error
   else
      ye(i) = sqrt(ye(i));
   end;
   
end;
